<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Models\SystemInfo;
use Illuminate\Support\Facades\DB;

/**
 * 系统消息
 */
class SystemInfoController extends Controller
{
    // 自动验证
    public $model = null;


    public function __construct()
    {
        parent::__construct();

        $this->model = new SystemInfo();
    }

    /**
     * 用户消息列表
     */
    public function systemList()
    {
        $user_id = $this->request->user_info['id'];

        $condition[] = ['user_id', '=', $user_id];

        //获取消息第一条
        $examine = $this->model->select('title', 'create_time')->where($condition)->whereIn('type', ['1', '3', '4', '5', '6', '7', '12', '13', '14', '15', '16', '17', '20', '24'])->orderByDesc('create_time')->first(); //审批列表
        $examine_num = $this->model->where($condition)->whereIn('type', ['1', '3', '4', '5', '6', '7', '12', '13', '14', '15', '16', '17', '20', '24'])->where('is_look', 2)->orderByDesc('create_time')->count(); //审批列表
        $pay = $this->model->select('title', 'create_time')->where($condition)->whereIn('type', ['22', '23', '33', '35', '36'])->orderByDesc('create_time')->first(); //支付
        $pay_num = $this->model->where($condition)->whereIn('type', ['22', '23', '33', '35'])->where('is_look', 2)->orderByDesc('create_time')->count(); //支付
        $system = $this->model->select('title', 'create_time')->where($condition)->whereIn('type', ['2', '8', '9', '10', '11', '18', '19', '21', '25', '26', '27', '28', '29', '30', '31', '32', '34'])->orderByDesc('create_time')->first(); //系统消息
        $system_num = $this->model->where($condition)->whereIn('type', ['2', '8', '9', '10', '11', '18', '19', '21', '25', '26', '27', '28', '29', '30', '31', '32', '34'])->where('is_look', 2)->orderByDesc('create_time')->count(); //系统消息

        return $this->returnApi(200, "查询成功", true, [
            'examine' => $examine,
            'examine_num' => $examine_num,
            'pay' => $pay,
            'pay_num' => $pay_num,
            'system' => $system,
            'system_num' => $system_num,
        ]);
    }

    /**
     * 获取用户分类消息列表
     * @param page 页数 默认为1
     * @param limit 限制条数 默认20
     * @param type 类型  1、用户审批信息列表  2、用户支付信息列表  3、用户系统消息
     */
    public function systemMessage()
    {
        $user_id = $this->request->user_info['id'];
        $type = $this->request->type;
        if (empty($type)) {
            return $this->returnApi(201, '参数错误');
        }
        if ($type == 1) {
            $type = [1, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16, 17, 20, 24];
        } elseif ($type == 2) {
            $type = [22, 23, 33, 35, 36];
        } else {
            $type = [2, 8, 9, 10, 11, 18, 19, 21, 25, 26, 27, 28, 29, 30, 31, 32, 34];
        }

        $condition[] = ['user_id', '=', $user_id];
        $page = $this->request->input('page', 1);
        $limit = $this->request->input('limit', 10);

        $res = $this->model->where($condition)->where(function ($query) use ($type) {
            if ($type) {
                $query->whereIn('type', $type);
            }
        })->orderByDesc('create_time')
            ->paginate($limit)
            ->toArray();

        if (empty($res['data'])) {
            return $this->returnApi(203, "暂无数据");
        }
        $condition[] = ['is_look', '=', 2];
        $this->model->where($condition)->update(['is_look' => 1, 'change_time' => date('Y-m-d H:i:s')]);

        $res = $this->disPageData($res);
        return $this->returnApi(200, "查询成功", "YES", $res);
    }
}
